<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;

class ModifyController extends Controller
{
    /**

     * 订单详情表
     * [getOrderdetail description]
     * @return [type] [description]
     */
    public function getOrderdetail(Request $request)
    {
        $id = $request->id;

        $ress = DB::table('orderdetails')
            ->join('orders','orderdetails.oid','=','orders.id')
            ->join('goods','orderdetails.gid','=','goods.id')
            ->join('shops','orderdetails.sid','=','shops.id')
            ->join('shopers','shops.spid','=','shopers.id')
            ->join('users','orders.uid','=','users.id')
            ->join('addresses','addresses.id','=','orders.addressid')
            ->where('orderdetails.oid','=',$id)
            ->select('orders.money','orders.ordernum','orders.id as oid','orders.createtime','orderdetails.count as gcount','shops.*','goods.*','shops.logo as splogo','shopers.phone','users.username','users.phone as userphone','addresses.*')
            ->get();
      // dd($ress);
      return view('home/UserCenter/orderdetail',['orders'=>$ress]);


    }

    /**
     * 删除订单
     * [getDel description]
     * @return [type] [description]
     */
    public function getDel(Request $request)
    {
        $id = $request->id;
        DB::table('orderdetails')->where('oid','=',$id)->delete();
        DB::table('orders')->where('id','=',$id)->delete();
        
        return back();
    }


    /**
     * 显示修改图片页面
     * [getImg description]
     * @return [type] [description]
     */
    public function getImg()
    {
      $id = session('id');
      $res = DB::table('users')->where('id','=',$id)->first()->headpic;

      return view('/home/UserCenter/img',['res'=>$res]);
    }

    public function postEditimg(Request $request)
    {
      $id = session('id');
      $oldpic = DB::table('users')->where('id','=',$id)->first()->headpic;

        if ($request->hasFile('avatar')) {
            if($request->file('avatar')->isValid()){
                
                $entension = $request -> file('avatar') -> getClientOriginalExtension();

                $name = mt_rand(0,9999).time().'.'.$entension;

                $request -> file('avatar')->move('home/UserCenters/img/default/',$name);

                if($oldpic && $oldpic != 'headpic.jpg') {
                    $aa = 'home/UserCenters/img/default/'.$oldpic;
                    unlink($aa); 
                }
               
                $res = DB::table('users')->where('id','=',$id)->update(['headpic'=>$name]);
                
                if($res){
                    return redirect('UserCenter/center');            
                }else{
                    return back();
                }
            }
        }

    }

    /**
     * 修改账户
     * 
     * [postEdituser description]
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function postEdituser(Request $request)
    {
       $id = session('id');
        

        $this->validate($request, [

              'username' => 'required|regex:/^\S{6,20}$/|unique:users,username',
          ],[
                  'username.required'=>'用户名必填',
                  'username.regex'=>'用户名格式不对',
                  'username.unique'=>'用户名已经存在'
          ]);

          DB::table('users')->where('id','=',$id)->update(['username'=>$request->username,'auth'=>1]);
      
            return back();
    }

    /**
     * 修改密码
     * [postModify description]
     * @return [type] [description]
     */
    public function postPassword(Request $request)
    {

        $id = session('id');
        $res = DB::table('users')->where('id','=',$id)->first()->password;

        if(!Hash::check($request->oldPwd,$res)){
            return back()->with('oldPwd','密码错误');
        }

        $this->validate($request, [

              'newPwd' => 'required|regex:/^\S{6,20}$/|same:confirm'
          ],[
                  'newPwd.required'=>'对不起密码必填',
                  'newPwd.regex'=>'密码格式不正确',
                  'newPwd.same'=>'两次密码不相同'
          ]);

        $password = Hash::make($request -> newPwd);
        $res = DB::table('users')->where('id','=',$id)->update(['password'=>"$password"]);

        if($res){
            return redirect('UserCenter/center');            
        }else{
            return back();
        }

    }

    /**
     *手机号修改页面 
     * 
     * [getPhone description]
     * @return [type] [description]
     */
    public function getPhone()
    {
      return view('/home/UserCenter/phone');
    }

    /**
     * 修改手机号
     * 
     * [postPhone description]
     * @return [type] [description]
     */
    public function postPhone(Request $request)
    {

      //查询数据库信息
      $id = session('id');
      $res = DB::table('users')->where('id','=',$id)->first()->phone;

      //判断原手机号是否正确
      if($request->oldphone != $res){
            return back()->with('oldphone','手机号错误');
        }

      //判断新的手机号规则
        $this->validate($request, [
              'newphone' => 'required|regex:/^\w{6,20}$/|unique:users,phone|same:rephone',
          ],[
                  'newphone.required'=>'对不起手机号必填',
                  'newphone.regex'=>'请输入正确手机号',
                  'newphone.unique'=>'该手机号已被注册',
                  'newphone.same'=>'两次输入不相同'
          ]);

      //执行修改
      $newsphone = DB::table('users')->where('id','=',$id)->update(['phone'=>"$request->newphone"]);
        
      //修改结果  
      if($newsphone){
        return redirect('UserCenter/center');

      }else{
        return back();
      }

    }

    /**
     * 修改邮箱页面
     * [getEmail description]
     * @return [type] [description]
     */
    public function getEmail()
    {
      return view('/home/UserCenter/email');
    }

    /**
     * 修改邮箱
     * 
     * [postEmail description]
     * @return [type] [description]
     */
    public function postEmail(Request $request)
    {

      $id = session('id');
      $res = DB::table('users')->where('id','=',$id)->first()->phone;

      $this -> validate($request,[
          'newemail' => 'required|regex:/^[0-9a-zA-Z]+@[0-9a-zA-Z\.]+[0-9a-zA-Z]{3,5}$/|unique:users,email|same:remail',
        ],[
            'newemail.required'=>'邮箱必填',
            'newemail.regex'=>'请输入正确的邮箱号',
            'newemail.same'=>'两次输入不同'
        ]);

        $newemail = DB::table('users')->where('id','=',$id)->update(['email'=>"$request->newemail"]);

        if($newemail){
            return redirect('UserCenter/center');
        }else{
           return back();
        }
      
    }


    /**
     *添加地址页面 
     * [getRaddress description]
     * @return [type] [description]
     */
    public function getRaddress()
    {
      if(empty(session('username'))){
        return back();
      }

      return view('/home/UserCenter/raddress');
    }

    /**
     * 执行地址添加
     * 
     * [postRaddress description]
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function postRaddress(Request $request)
    {
      $id = session('id');
      
      //判断输入规则      
      $this -> validate($request,[
          'name' => 'required',
          'place' => 'required|exists:base_area,name',
          'detail' => 'required',
          'phone' => 'required|regex:/^\w{6,20}$/',

        ],[
          'name.required' => '名称必填',
          'place.exists' => '必须为真实城市',
          'place.required' => '城市必填',
          'detail.required' => '详细地址必填',
          'phone.required' => '手机号必填',
          'phone.regex' => '请输入正确的手机号',
        ]);

        // 执行插入操作
        $raddress = DB::table('addresses')->insert([
            'uid' => Session('id'),
            'name' => $request->name,
            'city' => $request->place,
            'content' => $request->detail,
            'phone' => $request->phone,
            'sex' => $request->sex,
          ]);

        //判断是否成功
        if ($raddress) {
          return redirect('/UserCenter/address');
        }
    }

    /**
     * 删除地址
     * [getDelete description]
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function getDelete(Request $request)
    {
      DB::table('addresses')->where('id', '=', $request->id)->delete();
      return back();
      
    }

    /**
     * 地址修改页面
     * 
     * [getUpdate description]
     * @return [type] [description]
     */
    public function getUpdate(Request $request)
    {
      $id = $request->id;

      $addres = DB::select("select * from addresses where id='$id'");
      return view('/home/UserCenter/upaddress',['address'=>$addres]);
    }

    /**
    *执行地址修改
    */
    public function postUpdate(Request $request)
    {
      $id = $request->id;

      //判断输入规则      
      $this -> validate($request,[
          'name' => 'required',
          'place' => 'required|exists:base_area,name',
          'detail' => 'required',
          'phone' => 'required|regex:/^\w{6,20}$/',

        ],[
          'name.required' => '名称必填',
          'place.exists' => '必须为真实城市',
          'place.required' => '城市必填',
          'detail.required' => '详细地址必填',
          'phone.required' => '手机号必填',
          'phone.regex' => '请输入正确的手机号',
        ]);

      $update = DB::table('addresses')->where('id','=',$id)->update([
            'uid' => Session('id'),
            'name' => $request->name,
            'city' => $request->place,
            'content' => $request->detail,
            'phone' => $request->phone,
            'sex' => $request->sex,
        ]);

      if ($update) {
        return redirect('/UserCenter/address');
      }else{
        return redirect('/UserCenter/address');
      }
    }

}
